package 几数之和.两数之和和四数相加;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 四数相加
 * @date 2022/10/17 9:28
 */
public class Demo454 {

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer,Integer> map1=new HashMap();
        HashMap<Integer,Integer> map2=new HashMap();
        int count=0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                 map1.put(nums1[i]+nums2[j],map1.getOrDefault(nums1[i]+nums2[j],0)+1);
            }
        }
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                map2.put(nums3[i]+nums4[j],map2.getOrDefault(nums3[i]+nums4[j],0)+1);
            }
        }
        Set<Map.Entry<Integer, Integer>> entries = map1.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            if(map2.containsKey(-entry.getKey())){
                count=count+entry.getValue()*map2.get(-entry.getKey());
            }
        }
        return count;
    }
}
